package com.example.win.android_banduyujia.peopleactivity.activity.activity.openactivity.orderactivity.OrderDao;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.win.android_banduyujia.database.BaseDatabaseHelper;
import com.example.win.android_banduyujia.database.DBHelper;
import com.example.win.android_banduyujia.entity.OrderlistEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Win on 2015/9/21.
 */
public class MyOrderDatabase extends BaseDatabaseHelper {
    private static final String TABLE_NAME_MY_ORDER = "tb_my_order";

    private static MyOrderDatabase  mInstance;

    protected MyOrderDatabase(String tableName) {
        super(tableName);
    }

    public static MyOrderDatabase getInstance(){
        if(mInstance == null){
            mInstance = new MyOrderDatabase(TABLE_NAME_MY_ORDER);
        }
        return mInstance;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(String.format(SQL_ORDER_LIST_TABLE, TABLE_NAME_MY_ORDER));
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int currentVersion) {
        db.execSQL(String.format(DBHelper.SQL_ORDER_FORMAT, TABLE_NAME_MY_ORDER));
        onCreate(db);
    }

    public List<OrderlistEntity> getAllOrder() {
        SQLiteDatabase  db = null;
        Cursor  cursor = null;
        List<OrderlistEntity>  list = new ArrayList<OrderlistEntity>();

        try{
            db = DBHelper.getInstance().getReadableDatabase();

            cursor = db.query(TABLE_NAME_MY_ORDER, null, null, null, null, null, null);
            while (cursor.moveToNext()){
                OrderlistEntity  entity  = getOrderList(cursor);
                list.add(entity);
            }
            return list;
        }catch (Exception e){
            return list;
        }finally {
            if(cursor !=null)
                cursor.close();
            }
        }

}
